<?php
/**
 * ERROR CODE 0500-0599
 *
 * */
class FileManager
{
	private $error;

	public function __construct() {
		if( empty($this->error) ) {
			if( !empty($GLOBALS['error']) ) {
				$this->error = $GLOBALS['error'];
			} else {
				$this->error = new Error();
				$GLOBALS['error'] = &$this->error;
			}
		}
	}
	
	public function createFile($path,$num = 0) {
		if( $num !=0 )
			$myPath = $path." ({$num})";
		else
			$myPath = $path;
		if( !file_exists($myPath) ) {
			$res = touch($myPath);
			if( $res === false ) {
				$this->error->setError(0501,"can not create a file : \"{$myPath}\" ");
				return false;
			}
			return $myPath;
		} else {
			$num++;
			return $this->createFile($path,$num);
		}
	}

	public function createDir($path,$num = 0) {
		if( $num !=0 )
			$myPath = $path." ({$num})";
		else
			$myPath = $path;
		if( !file_exists($myPath) ) {
			$res = mkdir($myPath);
			if( $res === false ) {
				$this->error->setError(0501,"can not create a dir : \"{$myPath}\" ");
				return false;
			}
			return $myPath;
		} else {
			$num++;
			return $this->createDir($path,$num);
		}
	}
	
} 
?>